package com.chinda.modules.sys.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.chinda.common.annotation.SysLog;
import com.chinda.common.enums.MessageEnum;
import com.chinda.common.exception.RRException;
import com.chinda.common.utils.R;
import com.chinda.modules.sys.dto.GeneratorDTO;
import com.chinda.modules.sys.entity.GeneratorEntity;
import com.chinda.modules.sys.qo.GeneratorQO;
import com.chinda.modules.sys.service.GeneratorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;


/**
 * 项目示例
 *
 * @author chinda
 * @date 2021-06-20 07:18:54
 */
@AllArgsConstructor
@RestController
@RequestMapping("/generators")
@Api(tags = "项目示例")
public class GeneratorController {

    private final GeneratorService generatorService;

    /**
     * 分页
     *
     * @param qo
     * @return
     */
    @SysLog("查询项目示例分页")
    @PostMapping("/page")
    @ApiOperation(value = "查询分页信息")
    @PreAuthorize("hasPermission('/admin/userList','sys:user:info')")
    public R page(@ApiParam(value = "qo", name = "查询实例", required = true) @RequestBody GeneratorQO qo) {
        return R.ok().set(generatorService.page(qo));
    }

    /**
     * 获取详情
     *
     * @return
     */
    @SysLog("查询项目示例详情")
    @GetMapping("/{id}")
    @ApiOperation(value = "获取详情" )
    public R info(@PathVariable("id")  Integer id) {
        GeneratorEntity generator = generatorService.getById(id);
        return R.ok().set(generator);
    }

    /**
     * 保存
     *
     * @return
     */
    @SysLog("保存项目示例")
    @PostMapping
    @ApiOperation(value = "保存信息")
    public R save(@ApiParam(value = "generatorDTO", name = "项目示例DTO实例", required = true) @RequestBody @Valid GeneratorDTO generatorDTO) {
        GeneratorEntity generator = new GeneratorEntity();
        BeanUtil.copyProperties(generatorDTO, generator, CopyOptions.create().ignoreNullValue());
        generatorService.save(generator);
        return R.ok().set(generator);
    }

    /**
     * 修改全属性
     *
     * @param generatorDTO
     * @return
     */
    @SysLog("修改项目示例全属性")
    @PutMapping("/{id}")
    @ApiOperation(value = "修改全属性")
    public R modify(@PathVariable("id") Integer id,
                    @ApiParam(value = "generatorDTO", name = "项目示例DTO实例", required = true) @RequestBody @Valid GeneratorDTO generatorDTO) {
        GeneratorEntity generator = new GeneratorEntity();
        BeanUtil.copyProperties(generatorDTO, generator, CopyOptions.create().ignoreNullValue());
        generator.setId(id);
        if (!generatorService.updateById(generator)) {
            throw new RRException(MessageEnum.SYS_CHECK_0012.getMsg());
        }
        return R.ok().set(generator);
    }

    /**
     * 修改局部属性
     *
     * @param id
     * @param generatorDTO
     * @return
     */
    @SysLog("修改项目示例局部属性")
    @PatchMapping("/{id}")
    @ApiOperation(value = "修改局部属性")
    public R patch(@PathVariable("id") Integer id,
                   @ApiParam(value = "generatorDTO", name = "项目示例DTO实例", required = true) @RequestBody @Valid GeneratorDTO generatorDTO) {
        GeneratorEntity dbGenerator = generatorService.getById(id);
        if (dbGenerator == null) {
            throw new RRException(MessageEnum.SYS_CHECK_0022.getMsg());
        }
        BeanUtil.copyProperties(generatorDTO, dbGenerator, CopyOptions.create().ignoreNullValue());
        if (!generatorService.updateById(dbGenerator)) {
            throw new RRException(MessageEnum.SYS_CHECK_0012.getMsg());
        }
        return R.ok().set(dbGenerator);
    }

    /**
     * 删除数据
     *
     * @param id
     * @return
     */
    @SysLog("删除项目示例")
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除数据")
    public R remove(@PathVariable("id") Integer id) {
        generatorService.removeById(id);
        return R.ok();
    }
    /**
     * 批量删除数据
     *
     * @param ids
     * @return
     */
    @SysLog("批量删除项目示例")
    @DeleteMapping("/batch")
    @ApiOperation(value = "批量删除数据")
    public R remove(@ApiParam(value = "ids", name = "id集合", required = true) @RequestBody Integer [] ids) {
        generatorService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }
}
